﻿using Admin.Common;
using Admin.IService;
using Admin.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Service.Service
{
    /// <summary>
    /// 登陆操作
    /// </summary>
    public class LoginService : ILoginService
    {
        private ISysUserService _sysUserService;
        public LoginService(ISysUserService sysUserService)
        {
            _sysUserService = sysUserService;
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public async Task<SysUser> Login(string username, string password)
        {
            //object param, string sqlWhere
            SysUser user = await _sysUserService.Get(new { Account = username }, "Account=@Account");

            return Validate(user, password) ? user : null;
        }
        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="password">密码</param>
        public Boolean Validate(SysUser user, string password)
        {
            //获取加密串
            string md5 = EncryptionHelper.Md5(user.Account + password, user.Salt);
            return user.Pwd.Equals(md5);
        }

    }
}
